package com.flst.sql.db;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.flst.entity.Salle;
import com.flst.sql.MaBaseSQLite;
import com.flst.sql.SalleDao;

public class SalleDaoDb implements SalleDao {

	private SQLiteDatabase base;

	private static final String TABLE = "T_Salle";
	private static final String COL_ID = "ID";
	private static final String COL_NOM = "NOM";
	private static final String COL_CAPACITE = "CAPACITE";

	public SalleDaoDb(MaBaseSQLite bdd) {
		base = bdd.getReadableDatabase();
	}

	@Override
	public List<Salle> getAllSalles() {
		List<Salle> list_salles = new ArrayList<Salle>();

		Cursor c = base.query(TABLE, new String[] { COL_ID, COL_NOM,
				COL_CAPACITE }, null, null, null, null, null);

		if (c.moveToFirst()) {
			do {
				list_salles.add(cursorToSalle(c));
			} while (c.moveToNext());
		}
		c.close();

		return list_salles;
	}

	public Salle cursorToSalle(Cursor c) {
		Salle salle = new Salle();

		salle.setId(c.getLong(0));
		salle.setNom(c.getString(1));
		salle.setCapacite(c.getInt(2));

		return salle;
	}

	@Override
	public Salle getSalleById(Long id_salle) {

			Salle salle =  new Salle();
			
			Cursor c = base.query(TABLE, new String[] { COL_ID, COL_NOM,
					COL_CAPACITE }, COL_ID + " =" + id_salle, null, null, null, null);
			
			if (c.moveToFirst()) {
				do {
					salle = cursorToSalle(c);
				} while (c.moveToNext());
			}
			c.close();
			
			return salle;

	}

}
